<?php

class NewsController extends Zend_Controller_Action
{
    /**
     * Item id
     * @var integer
     */
    private $id = null;

    /**
     * Controller helper
     * @var Zend_Controller_Action_HelperBroker
     */
    private $itemsLoader = null;

    /**
     * News model
     * @var Application_Model_News
     */
    private $model = null;
    
    /**
     * Archive year
     * @var integer
     */
    private $archiveYear = 0;
    
    /**
     * Archive month
     * @var integer
     */
    private $archiveMonth = 0;
    
    /**
     * Archive day
     * @var integer
     */
    private $archiveDay = 0;

    /**
     * Order list
     * @var string
     */
    private $order = 'date DESC';
    
    /**
     * Limit of items
     * @var integer
     */
    private $limit = 2;

    /**
     * Current page
     * @var integer
     */
    private $pageNumber = 1;
    
    /**
     * List of items
     * @var array
     */
    private $items;
    
    /**
     * Current item
     * @var object 
     */
    private $item;
    
    /**
     * Count of items result
     * @var integer
     */
    private $count;
    
    /**
     * Count of pages
     * @var integer
     */
    private $pages;
    
    /**
     * Url for page
     * @var string
     */
    private $pageUrl = 'news';
    
    /**
     * Current page
     * @var Application_Model_Pages
     */
    private $page = null;
        
    /**
     * (non-PHPdoc)
     * @see Controller/Zend_Controller_Action::init()
     */
    public function init()
    {
    	$this->model = new Application_Model_News();
    	$this->itemsLoader = $this->_helper->itemsLoader($this->model);

		try
		{
	    	$this->page = $this->itemsLoader->loadPage($this->pageUrl);
    	}
		catch(Exception $e)
    	{
			throw new Zend_Controller_Exception($e->getMessage());
    	}
    	
    	$this->id = (int)$this->_getParam('id');
		$this->archiveYear = $this->_getParam('year');
    	$this->archiveMonth = $this->_getParam('month');
    	$this->archiveDay = $this->_getParam('day');
    	
    	$pageNumber = $this->_getParam('page');
    	
    	if (!empty($pageNumber))
    	{
    		$this->pageNumber = $pageNumber;
    	}
    }

    /**
     * Action for news index list page 
     */
    public function indexAction()
    {
    	$this->getList();
    }

    /**
     * Action for news item page
     */
    public function itemAction()
    {
    	if (isset($this->id))
    	{
    		try
    		{
	    		$this->item = $this->itemsLoader->getItem($this->id);
    		}
    		catch(Exception $e)
    		{
    			throw new Zend_Controller_Exception($e->getMessage());
    		}
    	}
    	else
    	{
    		throw new Zend_Controller_Exception("Can't find item with id #" . $this->id);
    	}	
    }

    public function editAction()
    {
		$form = new Application_Form_News(array( 
			'model' => 'News' 
		));

		/*
        $record = $this->_getRecord($id);

        $form->setRecord($record); 
 
        if($this->getRequest()->isPost() && $form->isValid()) 
        { 
            $record = $form->save(); 
            $this->_redirect('/show/' . $record->id); 
            exit; 
        }
        */
 
        $this->view->form = $form;
    }

    public function newAction()
    {
		$form = new Application_Form_News(array( 
			'model' => 'News' 
		));

		if ($this->getRequest()->isPost()) 
		{
	        $filter = new Zend_Filter_StripTags();

	        $title = trim($filter->filter($this->getRequest()->getPost('title')));
	
	        if ($title != '')
	        {
	            $data = array(
	                'title' => $title,
	            	'active' => 1
	            );
	            
	            $model = new Application_Model_News();
	            $model->getTable()->insert($data);
	            
	            $this->_redirect('/news');
	            return;
	        }
        }
 
        $this->view->form = $form; 
    }

    /**
     * Action for archive page
     */
    public function archiveAction()
    {
    	$dateIsset = false;
    	$errorMessage = "Incorrect date:" . 
		    			$this->archiveDay . "/" .
		    			$this->archiveMonth . "/" .
		    			$this->archiveYear;
    	
    	if ((!empty($this->archiveDay) && strlen($this->archiveDay) != 2) ||
    		(!empty($this->archiveMonth) && strlen($this->archiveMonth) != 2) ||
    		(!empty($this->archiveYear) && strlen($this->archiveYear) != 4)
    	)
    	{
    		throw new Zend_Controller_Exception($errorMessage);    		
    	}
    	else
    	{
			$this->archiveYear = (int)$this->archiveYear;
	    	$this->archiveMonth = (int)$this->archiveMonth;
	    	$this->archiveDay = (int)$this->archiveDay;
    	}
    	
    	if (!empty($this->archiveDay) && !empty($this->archiveMonth) && !empty($this->archiveYear))
    	{
    		$dateIsset = checkdate($this->archiveMonth, $this->archiveDay, $this->archiveYear);
    	}
    	else if (!empty($this->archiveYear))
    	{
    		$dateIsset = ($this->archiveYear > date("Y")) ? false : true;
    		
    		if (!empty($this->archiveMonth))
    		{
    			if (!($this->archiveMonth >= 1 && $this->archiveMonth <= 12))
    			{
    				$dateIsset = false;
    			}
    		}
    	}
    	
        if (!$dateIsset)
    	{
    		throw new Zend_Controller_Exception($errorMessage);    		
    	}
    	
    	if (!empty($this->archiveYear))
    	{
    		$fields['YEAR(date)'] = $this->archiveYear;
    	}
        if (!empty($this->archiveMonth))
    	{
    		$fields['MONTH(date)'] = $this->archiveMonth;
    	}
        if (!empty($this->archiveDay))
    	{
    		$fields['DAY(date)'] = $this->archiveDay;
    	}
    	
    	$this->getList($fields);
    }
    
    /**
     * Get news list
     * @param $fields
     */
    public function getList($fields = null)
    {
    	try
    	{
			$data = $this->itemsLoader->getList($fields, $this->order, $this->limit, $this->pageNumber);
    	}
       	catch(Exception $e)
    	{
    		throw new Zend_Controller_Exception($e->getMessage());
    	}
    	   	
    	$this->items = $data->items;
    	$this->count = $data->count;
    	$this->pages = ceil($data->count/$this->limit);
    }
    
    public function postDispatch()
    {
		$this->view = $this->itemsLoader->setPageParameters($this->view, $this->page);
    	$this->view->item	= $this->item;
    	$this->view->items	= $this->items;
    	$this->view->count	= $this->count;
    	$this->view->pageNumber	= $this->pageNumber;
    	$this->view->pages	= $this->pages;
    	$this->view->archiveDay = $this->archiveDay;
    	$this->view->archiveMonth = $this->archiveMonth;
    	$this->view->archiveYear = $this->archiveYear;
    }
}